package cc.linker.steplink.crm.controller.robot;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.commons.service.CommonsService;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchanth5.dao.SurvivalPushDao;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.service.ISurvivalService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 促活策略
 */

@Slf4j
@RestController
@RequestMapping("/survival")
public class SurvivalController {

    @Autowired
    private ISurvivalService survivalService;

    @Autowired
    private CommonsService commonsService;
    @Autowired
    private UserInfoUtil userInfoUtil;


    /**
     * 获取促活列表
     * @param session
     * @param title
     * @param trigger
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/getlist")
    public PageInfo getlist(HttpServletRequest request,
                            @RequestParam(value = "title", defaultValue = "", required = false) String title,
                            @RequestParam(value = "trigger", defaultValue = "0", required = false) int trigger,
                            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
                            @RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize
                            ){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        PageHelper.startPage(pageIndex, pageSize);
        String orgId = userinfo.getOrgId();
        Map<String,Object> param = new HashMap<>();
        param.put("orgId",orgId);
        param.put("isDelete",0);
        if(!StringUtils.isEmpty(title)){
            param.put("title",title);
        }
        if(!StringUtils.isEmpty(trigger) && trigger != 0){
            param.put("trigger",trigger);
        }
        return new PageInfo<>(survivalService.getList(param));
    }

    /**
     * 导出列表
     * @param session
     * @param response
     * @param title
     * @param trigger
     * @throws Exception
     */
    @GetMapping("/exportlist")
    public void exportList(HttpServletRequest request, HttpServletResponse response,
                           @RequestParam(value = "title", defaultValue = "", required = false) String title,
                           @RequestParam(value = "trigger", defaultValue = "0", required = false) int trigger
                            )throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        Map<String,Object> param = new HashMap<>();
        param.put("orgId",orgId);
        param.put("isDelete",0);
        if(!StringUtils.isEmpty(title)){
            param.put("title",title);
        }
        if(!StringUtils.isEmpty(trigger) && trigger != 0){
            param.put("trigger",trigger);
        }
        List<Map<String, Object>> list = survivalService.getList(param);
        for(Map<String, Object> da:list){
            if(StringUtils.isEmpty(da.get("trigger"))){
                da.put("action","");
            }else{
                String trigger1 = da.get("trigger").toString();
                String action;
                switch (trigger1){
                    case "1" :
                        action = "线上体检";
                        break;
                    case "2" :
                        action = "线下体检";
                        break;
                    case "3" :
                        action = "关注公众号:全部途径";
                        break;
                    case "6" :
                        action = "阅读文章";
                        break;
                    case "7" :
                        action = "点击公众号菜单";
                        break;
                    case "8" :
                        action = "扫描二维码";
                        break;
                    case "9" :
                        action = "关注公众号:一键关注";
                        break;
                    case "10" :
                        action = "关注公众号:扫码关注";
                        break;
                    default:
                        action = "";
                }
                da.put("action",action);
            }
            if(StringUtils.isEmpty(da.get("isStop"))){
                da.put("status","");
            }else{
                String isStop = da.get("isStop").toString();
                if("1".equals(isStop)){
                    da.put("status","停用中");
                }
                if("0".equals(isStop)){
                    da.put("status","进行中");
                }
            }
        }
        String[] key = {"title","action","touchNum","totalNum","status"};
        String[] eTitle = {"策略名称","触发条件","用户触发（次）","累计促活（次）","状态"};
        String fileName = "智能促活列表";
        commonsService.excelDownload(list,key,eTitle,fileName,response);
    }

    /**
     * 获取详情
     * @param session
     * @param id
     * @return
     */
    @GetMapping("/getdetail")
    public Map<String,Object> getDetail(HttpServletRequest request,@RequestParam(value = "id", defaultValue = "", required = false) String id){
        if(StringUtils.isEmpty(id)){
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String,Object> param = new HashMap<>(2);
        param.put("id",id);
        param.put("orgId",userinfo.getOrgId());
        return survivalService.getDetail(param);
    }

    /**
     * 获取编辑详情
     * @param session
     * @param id
     * @return
     */
    @GetMapping("/getinfo")
    public Map<String,Object> getInfo(HttpServletRequest request,String id)
    {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String,Object> param = new HashMap<>(2);
        param.put("id",id);
        param.put("orgId",userinfo.getOrgId());
        return survivalService.getInfo(param);
    }


    /**
     * 新增修改促活策略
     * @param session
     * @param map
     */
    @PostMapping("/addsurvival")
    public void AddSurvival(HttpServletRequest request, @RequestBody Map<String, Object> map)
    {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        map.put("orgId",orgId);
        // 验证标题是否重复
        Integer title = survivalService.getRepeatTitle(map);
        if(title>0){
            throw GlobalException.build("策略名称已存在，请重新输入");
        }
        survivalService.AddSurvival(map);
    }

    /**
     * 删除促活策略
     * @param session
     * @param id
     */
    @GetMapping("delsurvival")
    public void delSurvival(HttpServletRequest request,String id)
    {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String,Object> map = new HashMap<>();
        String orgId = userinfo.getOrgId();
        map.put("id",id);
        map.put("orgId",orgId);
        survivalService.delSurvival(map);
    }

    /**
     * 停用促活策略
     * @param session
     * @param id
     */
    @GetMapping("stopsurvival")
    public void stopSurvival(HttpServletRequest request,String id)
    {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String,Object> map = new HashMap<>();
        String orgId = userinfo.getOrgId();
        map.put("id",id);
        map.put("orgId",orgId);
        survivalService.stopSurvival(map);
    }

    /**
     * 公众号菜单列表
     * @param session
     */
    @GetMapping("/getMenuList")
    public List getMenuList(HttpServletRequest request)
    {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String,Object> map = new HashMap<>();
        String orgId = userinfo.getOrgId();
        map.put("orgId",orgId);
        return survivalService.getMenuList(map);
    }
    /**
     * 公众号菜单内容列表
     * @param menuId
     */
    @GetMapping("/getMenuDataList")
    public List getMenuDataList(Long menuId)
    {
        return survivalService.getMenuDataList(menuId);
    }

    /**
     * 根据菜单id删除促活
     * @param menuId
     */
    @PostMapping("/deleteByMenuId")
    public void deleteByMenuId(Long menuId)
    {
         survivalService.deleteByMenuId(menuId);
    }


}
